package com.phpxiu.app.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.phpxiu.app.api.OnDialogClickListener;
import com.phpxiu.app.config.Constants;
import com.phpxiu.app.config.HttpConfig;
import com.phpxiu.app.controller.avcontroller.QavsdkControl;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.CurLiveInfo;
import com.phpxiu.app.model.MySelfInfo;
import com.phpxiu.app.model.UserHomeLiveInfo;
import com.phpxiu.app.model.response.UserHomeModel;
import com.phpxiu.app.okhttp.OKHttp;
import com.phpxiu.app.okhttp.handler.OKHttpUIHandler;
import com.phpxiu.app.okhttp.param.OKHttpParam;
import com.phpxiu.app.utils.KKYUtil;
import com.phpxiu.app.view.dialog.NotificationOptionDlg;
import com.tencent.bugly.imsdk.crashreport.CrashReport;

/**
 * 点击通知统一跳转到此界面
 * Created by 吴金洲 on 2016/8/10
 */
public class NotificationOptionsWin extends UIBase {
    public final static String TAG="NotificationOptionsWin";
    public final static String ACTION_CHAT="action_chat";
    public final static String ACTION_LIVE="action_live";
    public final static String EXTRA_ACTION_KEY="extra_action_key";
    public final static String EXTRA_USER_ID="extra_user_id";
    private NotificationOptionDlg dlg;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String action=getIntent().getStringExtra(EXTRA_ACTION_KEY);
        if(action==null||"".equals(action)){
            finish();
            return;
        }
        dlg=new NotificationOptionDlg(this);
        dlg.setOwnerActivity(this);
        if(ACTION_CHAT.equals(action)){
            showC2CConversation();
        }
        if(ACTION_LIVE.equals(action)){
            showOptionLive(getIntent().getStringExtra(EXTRA_USER_ID));
        }
    }

    /**
     * 从推送消息跳转到会话
     */
    private void showC2CConversation(){
        Intent c2c=new Intent(this,C2CConversationManager.class);
        startActivity(c2c);
        finish();
    }

    /**
     * 从推送消息显示是否前往观看直播
     */
    private void showOptionLive(final String uid){
        dlg.setTitle("观看直播");
        dlg.setDescription("确定前往观看直播");
        dlg.setOnDialogClickListener(new OnDialogClickListener() {
            @Override
            public void onOk() {
                gotoLiveRoom(uid);
            }

            @Override
            public void onCancel() {
                finish();
            }
        });
        dlg.show();
    }

    /**
     * 跳转到直播
     */
    private void gotoLiveRoom(String uid){
        if(CurLiveInfo.isInRoom||uid==null||"".equals(uid)){
            KKYUtil.log("", "无法进入房间!");
            finish();
            return;
        }
        OKHttpParam param=OKHttpParam.builder(false);
        param.put("uid",uid);
        OKHttp.post(HttpConfig.API_USER_HOME, param.jsonParam(), TAG, new OKHttpUIHandler(UserHomeModel.class) {
            @Override
            public void onSuccess(Object model) {
                UserHomeModel m = (UserHomeModel) model;
                KKYUtil.log("主播中心数据：" + m.getResponseStr());
                try {
                    UserHomeLiveInfo liveInfo = m.getData().getLive();
                    if ("1".equals(liveInfo.getStatus())) {
                        onEnterRoom(liveInfo);
                    } else {
                        finish();
                    }
                } catch (NullPointerException e) {
                    finish();
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onErr(String errMsg) {
                //mainView.showTopMsg(errMsg);
                finish();
            }
        });
    }


    private void onEnterRoom(UserHomeLiveInfo liveInfo){
        KKYUtil.log(TAG,"进入直播间："+liveInfo.getAvRoomId()+"@"+liveInfo.getTitle());
        //SDK实例存在
        if(QavsdkControl.getInstance().getAVContext()==null){
            Toast.makeText(this, "进入失败，请稍后再试", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
        try {
            //如果是自己
            if (liveInfo.getHost().getUid().equals(MySelfInfo.getInstance().getId())) {
                Toast.makeText(this, "房间不存在!", Toast.LENGTH_SHORT).show();
                finish();
                return;
            }
            Intent intent = new Intent(this, RoomVideoRoom.class);
            intent.putExtra(Constants.ID_STATUS, Constants.MEMBER);
            MySelfInfo.getInstance().setIdStatus(Constants.MEMBER);
            CurLiveInfo.setHostID(liveInfo.getHost().getUid());
            CurLiveInfo.setHostName(liveInfo.getHost().getNickname());
            CurLiveInfo.setHostAvator(liveInfo.getHost().getAvatar());
            CurLiveInfo.setRoomNum(Integer.parseInt(liveInfo.getAvRoomId()));
            CurLiveInfo.setMembers(Integer.parseInt(liveInfo.getWatchCount()) + 1); // 添加自己
            CurLiveInfo.setAdmires(Integer.parseInt(liveInfo.getAdmireCount()));
            CurLiveInfo.setAddress(liveInfo.getLbs().getAddress());
            startActivity(intent);
            finish();
        }catch (Exception e){
            finish();
            e.printStackTrace();
        }
    }
}
